<?php
declare(strict_types=1);

namespace app\common\model\mysql;

use think\Model;

class BaseModel extends Model
{
    protected $autoWriteTimestamp = true;

    /**
     * 根据分类名获取分类信息
     *
     * @param string $name
     * @return Category|array|false|Model|null
     * @throws \think\db\exception\DataNotFoundException
     * @throws \think\db\exception\DbException
     * @throws \think\db\exception\ModelNotFoundException
     */
    public function getDataByName(string $name = null)
    {
        if (empty($name)) return false;

        return $this->where('name', $name)->find();
    }

    /**
     * 通过 ID 数组获取数据信息
     *
     * @param array $idArr
     * @param bool $field
     * @return BaseModel[]|array|\think\Collection
     * @throws \think\db\exception\DataNotFoundException
     * @throws \think\db\exception\DbException
     * @throws \think\db\exception\ModelNotFoundException
     */
    public function getNormalByIdArr(array $idArr, $field = true)
    {
        $where = ['status' => config('status.mysql.table_normal')];
        $res   = $this->field($field)->whereIn('id', $idArr)->where($where)->select();
        return $res;
    }

    /**
     * 根据条件获取对应数据
     *
     * @param array $condition
     * @param array|string[] $order
     * @return BaseModel[]|array|\think\Collection
     * @throws \think\db\exception\DataNotFoundException
     * @throws \think\db\exception\DbException
     * @throws \think\db\exception\ModelNotFoundException
     */
    public function getDataByCondition(array $condition, array $order = ['id' => 'desc'])
    {
        if (!$condition) return [];
        $result = $this->where($condition)->order($order)->select();
        return $result;
    }
}